package userServlet;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import userDao.UserDao;
import userEntity.User;
import userImplment.UserImplement;

/**
 * Servlet implementation class Pay
 */
public class Pay extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Pay() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		int money=Integer.parseInt(request.getParameter("money"));
		
		HttpSession session = request.getSession();
		//从session获得用户名
		User user=(User) session.getAttribute("user");
	    UserDao userImplement=new UserImplement();
		PrintWriter out=response.getWriter();
		if(user==null){
			out.write("false");
		}else{
			if(userImplement.Pay(user.getId(),money)) {
				//支付成功，重置余额到session，并跳转到我的信息页面
				user.setBalance(userImplement.Query(user.getId()).getBalance());
				session.setAttribute("user",user);
				out.write("true");
				//request.getRequestDispatcher("myQuery.jsp").forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
